package CtrlLayer;
import java.util.ArrayList;
import ModelLayer.*;

public class DeliveryController
{
    private DeliveryContainer deliveryContainer;
    
    public DeliveryController()
    {
        deliveryContainer = DeliveryContainer.getInstance();
    }

    public int createDelivery(int customerID,String customerAddress, String customerPhone, double price, int sessionId){  
        int employeeID = SessionContainer.getInstance().findSessionById(sessionId).getPersonId();
        Delivery dObject = null;
        int index = 0;
        int id = 0;
        while(index < deliveryContainer.getDeliveryList().size())
        {
            dObject = deliveryContainer.getDeliveryList().get(index);
            if(dObject != null){
                if(dObject.getDeliveryID()>id)   
                    id = dObject.getDeliveryID();
            }else{
                id = 1;
            }
            index++;
        }
        id++;
        if(CustomerContainer.getInstance().checkCustomer(customerID)){
            deliveryContainer.addDelivery(new Delivery(id, employeeID, customerID, customerAddress, customerPhone,price));
            return id;
        } else {
            return 0;
        }
    }

    public boolean addProductInDelivery(int id, int productBarcode, int amount){
        if(deliveryContainer.findDeliveryById(id) != null) {
            deliveryContainer.findDeliveryById(id).addProduct(productBarcode,amount);
            return true;
        } else {
            return false;
        }
    }

    public boolean removeProductFromDelivery(int id, int productBarcode,int amount){
        if(deliveryContainer.findDeliveryById(id) != null) {
            deliveryContainer.findDeliveryById(id).removeProduct(productBarcode,amount);
            return true;
        } else {
            return false;
        }
    }

    public Delivery findDelivery(int id){    
        return  deliveryContainer.findDeliveryById(id);
    }

    public void deleteDelivery(int id){
        deliveryContainer.deleteDelivery(deliveryContainer.findDeliveryById(id));
    }

    public void updateSale(int id){
       
    }

}
